home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Memphis Amiga Group / MAG DOS 2.0 Utilities Disk 01 (1991-09)(Memphis Amiga Group).zip / MAG DOS 2.0 Utilities Disk 01 (1991-09)(Memphis Amiga Group).adf / Back&Front / toBack&Front.c < prev    next >
C/C++ Source or Header  |  1991-08-17  |  24KB  |  702 lines

  1. /*
  2.  *  toBack&Front.c
  3.  *
  4.  *  Commodity
  5.  *
  6.  *  Author: Stefan Sticht
  7.  *
  8.  *  Copyright: source is public domain, no copyright
  9.  *
  10.  *  Version history:
  11.  *
  12.  *  V1.00   initial release
  13.  *  V1.01   some minor changes
  14.  *  V1.02   changed myparseix for commodities.library 37.27
  15.  *          uses now utility.library's Stricmp()
  16.  *  V1.03   added some LockIBase()
  17.  */
  18.  
  19. #define VERSION "V1.03"
  20.  
  21. /********************************************************************
  22.  *                             interfacing                          *
  23.  ********************************************************************/
  24.  
  25. /*
  26.  *  include files
  27.  */
  28.  
  29. #include <stdarg.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <intuition/intuitionbase.h>
  33. #include <libraries/commodities.h>
  34.  
  35. #include <clib/alib_protos.h>
  36. #include <clib/commodities_protos.h>
  37. #include <pragmas/commodities_pragmas.h>
  38. #include <clib/dos_protos.h>
  39. #include <pragmas/dos_pragmas.h>
  40. #include <clib/exec_protos.h>
  41. #include <pragmas/exec_pragmas.h>
  42. #include <clib/intuition_protos.h>
  43. #include <pragmas/intuition_pragmas.h>
  44. #include <clib/layers_protos.h>
  45. #include <pragmas/layers_pragmas.h>
  46. #include <clib/utility_protos.h>
  47. #include <pragmas/utility_pragmas.h>
  48.  
  49. #ifdef DEBUG
  50. #define printf KPrintF
  51. #include <clib/dlib_protos.h>
  52. #endif
  53.  
  54. /*
  55.  *  prototypes
  56.  */
  57. struct Library *myopenlibrary(char *name, unsigned long version);
  58. void processmessages(void);
  59. void backorfront(unsigned short mode);
  60.  
  61. /* located in myparseix.c */
  62. long myparseix(char *description, IX *ix);
  63.  
  64. /*
  65.  *  global data defined in other moduls
  66.  *
  67.  *  libraries opened by startup code; basepointers needed by function pragmas
  68.  */
  69. extern struct Library *DOSBase;
  70. extern struct Library *SysBase;
  71.  
  72. /*
  73.  *  Disable SAS/C CTRL/C handling
  74.  */
  75. void chkabort(void) {}
  76.  
  77. /********************************************************************
  78.  *                             global data                          *
  79.  ********************************************************************/
  80.  
  81. /*
  82.  *  definition of messages
  83.  */
  84. #ifdef GERMAN
  85. /*
  86.  *  Wenn jemand bessere Übersetzungsvorschläge hat, soll er mir die bitte mitteilen!
  87.  *  (This is german. If you don't understand, doesn't matter)
  88.  */
  89. #define COM_NAME                "toBack&Front"
  90. #define RETRY_GADGETS           "Wiederholen|Abbrechen"
  91. #define RESUME_GADGETS          "Weiter"
  92. #define MSG_LIBRARY_OPENERR     "Die %s (V%ld+) kann nicht geöffnet werden!"
  93. #define COM_DESCR               "Fenster o. Schirm nach hinten o. vorne"
  94. #define YES                     "JA"
  95. #define NO                      "NEIN"
  96.  
  97. #else
  98.  
  99. #define COM_NAME                "toBack&Front"
  100. #define RETRY_GADGETS           "Retry|Cancel"
  101. #define RESUME_GADGETS          "Resume"
  102. #define MSG_LIBRARY_OPENERR     "%s (V%ld+) can't be opened!"
  103. #define COM_DESCR               "Window or screen to back or front"
  104. #define YES                     "YES"
  105. #define NO                      "NO"
  106.  
  107. #endif
  108.  
  109. #define COM_TITLE               COM_NAME " " VERSION
  110. #define CX_PRIORITY             "CX_PRIORITY"
  111. #define DEF_CX_PRIORITY         0
  112.  
  113. #define TT_FRONT_ACTION         "FRONT_ACTION"
  114. #define TT_FRONT_CLICKS         "FRONT_CLICKS"
  115. #define TT_FRONT_REMOVE         "FRONT_REMOVE"
  116. #define DEF_TT_FRONT_ACTION     "rawmouse leftbutton lbuttoncode"
  117. #define DEF_TT_FRONT_CLICKS     2
  118. #define DEF_TT_FRONT_REMOVE     NO
  119. #define TT_BACK_ACTION          "BACK_ACTION"
  120. #define TT_BACK_CLICKS          "BACK_CLICKS"
  121. #define TT_BACK_REMOVE          "BACK_REMOVE"
  122. #define DEF_TT_BACK_ACTION      "rawmouse lalt leftbutton lbuttoncode"
  123. #define DEF_TT_BACK_CLICKS      2
  124. #define DEF_TT_BACK_REMOVE      YES
  125.  
  126. /*
  127.  *  data for cback.o
  128.  */
  129. #ifndef DEBUG
  130. long _stack = 2048l;
  131. char *_procname = COM_NAME;
  132. long _priority = 21l;
  133. long _BackGroundIO = 1;
  134. extern BPTR _Backstdout;
  135. #endif
  136.  
  137. /*
  138.  *  library base pointers
  139.  */
  140. struct IntuitionBase *IntuitionBase;
  141. struct Library *CxBase;
  142. struct Library *IconBase;
  143. struct Library *LayersBase;
  144. struct Library *UtilityBase;
  145.  
  146. /*
  147.  *  message ports
  148.  */
  149. struct MsgPort *cxport;
  150.  
  151. /*
  152.  *  signal flags
  153.  */
  154. unsigned long cxsigflag;
  155.  
  156. /*
  157.  *  programtitle and version for Version command
  158.  */
  159. char versionstring[] ="\0$VER: " COM_NAME " " VERSION;
  160.  
  161. /*
  162.  *  helpstring
  163.  */
  164. #ifdef GERMAN
  165. char helpstring[] = "\033[1m" COM_NAME "\033[0m " VERSION " (Public Domain) von Stefan Sticht\n"\
  166.                     "Aufruf: " COM_NAME " [" CX_PRIORITY "=<n>] [" TT_FRONT_ACTION "=<Aktion>]"\
  167.                     " [" TT_FRONT_REMOVE "=" YES "|" NO "] [" TT_FRONT_CLICKS "=<n>] ["\
  168.                     TT_BACK_ACTION "=<Aktion>] [" TT_BACK_REMOVE "=" YES "|" NO
  169.                     "] [" TT_BACK_CLICKS "=<n>]\n";
  170. #else
  171. char helpstring[] = "\033[1m" COM_NAME "\033[0m " VERSION " (Public Domain) by Stefan Sticht\n"
  172.                     "Usage: " COM_NAME " [" CX_PRIORITY "=<n>] [" TT_FRONT_ACTION "=<action>]"\
  173.                     " [" TT_FRONT_REMOVE "=" YES "|" NO "] [" TT_FRONT_CLICKS "=<n>] ["\
  174.                     TT_BACK_ACTION "=<action>] [" TT_BACK_REMOVE "=" YES "|" NO
  175.                     "] [" TT_BACK_CLICKS "=<n>]\n";
  176. #endif
  177.  
  178. /*
  179.  *  number of clicks required
  180.  */
  181. unsigned char requiredbackclicks;
  182. unsigned char requiredfrontclicks;
  183.  
  184. #define FRONT 1
  185. #define BACK  2
  186.  
  187. /*
  188.  *  the tooltypearray
  189.  */
  190. char **tooltypes;
  191.  
  192. /*
  193.  *  our broker
  194.  */
  195. CxObj *broker;
  196.  
  197. struct NewBroker newbroker = {
  198.     NB_VERSION,                         /* BYTE nb_Version               */
  199.     COM_NAME,                           /* BYTE *nb_Name                 */
  200.     COM_TITLE,                          /* BYTE *nb_Title                */
  201.     COM_DESCR,                          /* BYTE *nb_Descr                */
  202.     NBU_NOTIFY | NBU_UNIQUE,            /* SHORT nb_Unique               */
  203.     0,                                  /* SHORT nb_Flags                */
  204.     0,                                  /* BYTE nb_Pri                   */
  205.     NULL,                               /* struct MsgPort nb_Port        */
  206.     0                                   /* WORD nb_ReservedChannel       */
  207. };
  208.  
  209. IX backix = {
  210.     IX_VERSION,                 /* UBYTE ix_version     */
  211.     0,                          /* UBYTE ix_Class       */
  212.     0,                          /* UWORD ix_Code        */
  213.     0,                          /* UWORD ix_CodeMask    */
  214.     0,                          /* UWORD ix_Qualifier   */
  215.     0,                          /* UWORD ix_QualMask    */
  216.     0                           /* UWORD ix_QualSame    */
  217.     };
  218.  
  219. IX frontix = {
  220.     IX_VERSION,                 /* UBYTE ix_version     */
  221.     0,                          /* UBYTE ix_Class       */
  222.     0,                          /* UWORD ix_Code        */
  223.     0,                          /* UWORD ix_CodeMask    */
  224.     0,                          /* UWORD ix_Qualifier   */
  225.     0,                          /* UWORD ix_QualMask    */
  226.     0                           /* UWORD ix_QualSame    */
  227.     };
  228.  
  229. /********************************************************************
  230.  *                             functions                            *
  231.  ********************************************************************/
  232.  
  233. /*
  234.  *  request(): a glue routine to EasyRequest as simple as printf plus
  235.  *             titlestring, gadgettexts
  236.  *
  237.  *  Input: char *title:         pointer to the title of the requester
  238.  *         char *gadgets:       pointer to gadgettext
  239.  *         char *text:          text displayed in requester
  240.  *
  241.  *  Result: same as EasyrequestArgs()
  242.  *
  243.  * !!! for more info see EasyRequestArgs() in Autodocs/intuition.doc !!!
  244.  */
  245. long request(char *title, char *gadgets, char *text, ...)
  246. {
  247.     /*
  248.      *  structure textreq only needed in this function, so hide it here
  249.      *  must be static, in order to be initialized only once
  250.      */
  251.     static struct EasyStruct textreq = {
  252.         sizeof (struct EasyStruct), /* ULONG es_StructSize      */
  253.         0l,                         /* ULONG es_Flags           */
  254.         NULL,                       /* UBYTE *es_Title          */
  255.         NULL,                       /* UBYTE *es_TextFormat     */
  256.         NULL,                       /* UBYTE *es_GadgetFormat   */
  257.         };
  258.     va_list ap;
  259.     long rc;
  260.  
  261.     /*
  262.      *  get start of variable arguments
  263.      */
  264.     va_start(ap, text);
  265.  
  266.     /*
  267.      *  update textreq
  268.      */
  269.     textreq.es_Title = (UBYTE *)title;
  270.     textreq.es_TextFormat = (UBYTE *)text;
  271.     textreq.es_GadgetFormat = (UBYTE *)gadgets;
  272.  
  273.     /*
  274.      *  win may be NULL
  275.      */
  276.     rc = EasyRequestArgs(NULL, &textreq, NULL, ap);
  277.  
  278.     va_end(ap);
  279.  
  280.     return(rc);
  281. }
  282.  
  283. /*
  284.  *  myopenlibrary(): same as OpenLibrary(), but opens a retry-requester
  285.  *                   if OpenLibrary() fails, to give the user a chance to
  286.  *                   copy the library to libs: and retry
  287.  *                   requires request(), see above
  288.  */
  289. struct Library *myopenlibrary(char *name, unsigned long version)
  290. {
  291.     static char errortext[] = MSG_LIBRARY_OPENERR;
  292.     struct Library *libptr;
  293.     long ok = TRUE;
  294.  
  295.     do {
  296.         if (!(libptr = OpenLibrary((UBYTE *)name, version))) {
  297.             if (IntuitionBase) {
  298.                 ok = request(COM_NAME ":", RETRY_GADGETS, errortext, name, version);
  299.                 }
  300.             else ok = FALSE;
  301.             }
  302.         } while (!libptr && ok);
  303.  
  304.     #ifdef DEBUG
  305.     printf("myopenlibrary(%s, %ld) = 0x%lx\n", name, version, libptr);
  306.     #endif
  307.     return(libptr);
  308. }
  309.  
  310. void main(int argc, char *argv[])
  311. {
  312.     CxObj *obj;
  313.     CxObj *frontfilter = NULL;
  314.     CxObj *backfilter = NULL;
  315.     char *frontaction;
  316.     char *backaction;
  317.     struct Message *msg;
  318.  
  319.     if ((argc > 1) && (*argv[1] == '?')) {
  320.         /*
  321.          *  display help string
  322.          */
  323.         #ifndef DEBUG
  324.         if (_Backstdout) {
  325.             Write(_Backstdout, helpstring, sizeof(helpstring) - 1l);
  326.             Close(_Backstdout);
  327.             }
  328.         #else
  329.         Write(Output(), helpstring, sizeof(helpstring) - 1l);
  330.         #endif
  331.         return;
  332.         }
  333.     #ifndef DEBUG
  334.     else if (argc && _Backstdout) Close(_Backstdout);
  335.     #endif
  336.  
  337.     /*
  338.      *  open required libraries first
  339.      */
  340.     if (IntuitionBase = (struct IntuitionBase *)myopenlibrary("intuition.library", 37l)) {
  341.  
  342.         if (CxBase = myopenlibrary("commodities.library", 37l)) {
  343.  
  344.             if (IconBase = myopenlibrary("icon.library", 37l)) {
  345.  
  346.                 if (LayersBase = myopenlibrary("layers.library", 37l)) {
  347.  
  348.                     /*
  349.                      * create tooltypes array (requires icon.library open!!!)
  350.                      */
  351.                     tooltypes = (char **)ArgArrayInit(argc, argv);
  352.  
  353.                     backaction = ArgString(tooltypes, TT_BACK_ACTION, DEF_TT_BACK_ACTION);
  354.                     frontaction = ArgString(tooltypes, TT_FRONT_ACTION, DEF_TT_FRONT_ACTION);
  355.                     if (backaction && *backaction) {
  356.                         myparseix(backaction, &backix);
  357.                         requiredbackclicks = (unsigned char)ArgInt(tooltypes, TT_BACK_CLICKS, DEF_TT_BACK_CLICKS);
  358.                         }
  359.                     else backaction = NULL;
  360.                     if (frontaction && *frontaction) {
  361.                         myparseix(frontaction, &frontix);
  362.                         requiredfrontclicks = (unsigned char)ArgInt(tooltypes, TT_FRONT_CLICKS, DEF_TT_FRONT_CLICKS);
  363.                         }
  364.                     else frontaction = NULL;
  365.  
  366.                     if (frontaction || backaction) {
  367.                         /*
  368.                          *  create our message port
  369.                          */
  370.                         if (cxport = CreateMsgPort()) {
  371.  
  372.                             cxsigflag = 1l << cxport->mp_SigBit;
  373.                             /*
  374.                              * set up some broker data
  375.                              */
  376.                             newbroker.nb_Pri = ArgInt(tooltypes, CX_PRIORITY, DEF_CX_PRIORITY);
  377.                             newbroker.nb_Port = cxport;
  378.  
  379.                             if (broker = CxBroker(&newbroker, NULL)) {
  380.  
  381.                                 if (frontaction && (frontfilter = CxFilter(NULL))) {
  382.                                     AttachCxObj(broker, frontfilter);
  383.                                     SetFilterIX(frontfilter, &frontix);
  384.                                     }
  385.  
  386.                                 if (backaction && (backfilter = CxFilter(NULL))) {
  387.                                     AttachCxObj(broker, backfilter);
  388.                                     SetFilterIX(backfilter, &backix);
  389.                                     }
  390.  
  391.                                 if (frontfilter || backfilter) {
  392.  
  393.                                     if (UtilityBase = myopenlibrary("utility.library", 37l)) {
  394.  
  395.                                         if (frontfilter && (obj = CxSender(cxport, FRONT))) {
  396.                                             AttachCxObj(frontfilter, obj);
  397.                                             /*
  398.                                              *  check if input-event shall be removed
  399.                                              */
  400.                                             if (!Stricmp((char *)ArgString(tooltypes, TT_FRONT_REMOVE, DEF_TT_FRONT_REMOVE), YES)) {
  401.                                                 #ifdef DEBUG
  402.                                                 printf("main(): installing NULL-translator for front-action\n");
  403.                                                 #endif
  404.                                                 /*
  405.                                                  *  install a NULL-translator, which removes the ie
  406.                                                  */
  407.                                                 if (obj = CxTranslate(NULL)) AttachCxObj(frontfilter, obj);
  408.                                                 }
  409.                                             }
  410.  
  411.                                         if (backfilter && (obj = CxSender(cxport, BACK))) {
  412.                                             AttachCxObj(backfilter, obj);
  413.                                             /*
  414.                                              *  check if input-event shall be removed
  415.                                              */
  416.                                             if (!Stricmp((char *)ArgString(tooltypes, TT_FRONT_REMOVE, DEF_TT_FRONT_REMOVE), YES)) {
  417.                                                 #ifdef DEBUG
  418.                                                 printf("main(): installing NULL-translator for front-action\n");
  419.                                                 #endif
  420.                                                 /*
  421.                                                  *  install a NULL-translator, which removes the ie
  422.                                                  */
  423.                                                 if (obj = CxTranslate(NULL)) AttachCxObj(backfilter, obj);
  424.                                                 }
  425.                                             }
  426.  
  427.                                         /*
  428.                                          *  close library here, we don't need it any longer
  429.                                          */
  430.                                         CloseLibrary(UtilityBase);
  431.  
  432.                                         if ((frontfilter && !CxObjError(frontfilter)) ||
  433.                                             (backfilter && !CxObjError(backfilter))) {
  434.                                             /*
  435.                                              *  activate our commodity
  436.                                              */
  437.                                             ActivateCxObj(broker, 1l);
  438.                                             /*
  439.                                              *  now watch our numerous ports
  440.                                              */
  441.                                             processmessages();
  442.  
  443.                                             } /* if !CxObjError() */
  444.  
  445.                                         } /* if UtilityBase */
  446.  
  447.                                     } /* if frontfilter || backfilter */
  448.  
  449.                                 DeleteCxObjAll(broker);
  450.  
  451.                                 } /* if broker */
  452.  
  453.                             #ifdef DEBUG
  454.                             else printf("main(): CxBroker() failed!\n");
  455.                             #endif
  456.  
  457.                             /*
  458.                              *  delete our message port after replying all pending messages
  459.                              */
  460.                             while (msg = GetMsg(cxport)) ReplyMsg(msg);
  461.                             DeleteMsgPort(cxport);
  462.                             } /* if cxport */
  463.  
  464.                         #ifdef DEBUG
  465.                         else printf("main(): CreateMsgPort() failed!\n");
  466.                         #endif
  467.  
  468.                         } /* if (frontaction || backaction) */
  469.  
  470.                     ArgArrayDone();
  471.  
  472.                     CloseLibrary(LayersBase);
  473.                     } /* if LayersBase */
  474.  
  475.                 CloseLibrary(IconBase);
  476.                 } /* if IconBase */
  477.  
  478.             CloseLibrary(CxBase);
  479.             } /* if CxBase */
  480.  
  481.         CloseLibrary((struct Library *)IntuitionBase);
  482.         } /* if IntuitionBase */
  483.  
  484. } /* main() */
  485.  
  486. void processmessages(void)
  487. {
  488.     static unsigned long lastfrontmicros = 0l;
  489.     static unsigned long lastfrontsecs = 0l;
  490.     static unsigned long lastbackmicros = 0l;
  491.     static unsigned long lastbacksecs = 0l;
  492.     static unsigned char frontclicks = 0;
  493.     static unsigned char backclicks = 0;
  494.     struct InputEvent *ie;
  495.     struct Message *msg;
  496.     unsigned long sigreceived;
  497.     unsigned long msgtype;
  498.     unsigned long msgid;
  499.     unsigned long micros;
  500.     unsigned long secs;
  501.     unsigned char quit = FALSE;
  502.  
  503.     if (requiredfrontclicks == 1) frontclicks = 1;
  504.     if (requiredbackclicks == 1) backclicks = 1;
  505.  
  506.     while (!quit) {
  507.  
  508.         sigreceived = Wait(SIGBREAKF_CTRL_C | cxsigflag);
  509.  
  510.         #ifdef DEBUG
  511.         printf("processmessages(): signal received\n");
  512.         #endif
  513.  
  514.         if (sigreceived & SIGBREAKF_CTRL_C) quit = TRUE;
  515.  
  516.         if (sigreceived & cxsigflag) {
  517.  
  518.             while (msg = (struct Message *)GetMsg(cxport)) {
  519.  
  520.                 msgid = CxMsgID((CxMsg *)msg);
  521.                 msgtype = CxMsgType((CxMsg *)msg);
  522.                 
  523.                 if ((msgtype == CXM_IEVENT) &&
  524.                     (ie = (struct InputEvent *)CxMsgData((CxMsg *)msg))) {
  525.                     /*
  526.                      *  copy the interesting data of the inputevent
  527.                      */
  528.                     secs = ie->ie_TimeStamp.tv_secs;
  529.                     micros = ie->ie_TimeStamp.tv_micro;
  530.                     #ifdef DEBUG
  531.                     if (msgid == FRONT) printf("action = FRONT\n");
  532.                     else printf("action = BACK\n");
  533.                     printf("ie_Class = 0x%lx\n", ie->ie_Class);
  534.                     printf("ie_SubClass = 0x%lx\n", ie->ie_SubClass);
  535.                     printf("ie_Code= 0x%lx\n", ie->ie_Code);
  536.                     printf("ie_Qualifier = 0x%lx\n", ie->ie_Qualifier);
  537.                     printf("secs = 0x%lx\n", secs);
  538.                     printf("micros = 0x%lx\n", micros);
  539.                     #endif
  540.                     }
  541.  
  542.                 ReplyMsg(msg);
  543.  
  544.                 switch (msgtype) {
  545.  
  546.                     case CXM_IEVENT:
  547.                         switch (msgid) {
  548.  
  549.                             case FRONT:
  550.                                 if (requiredfrontclicks > 1) {
  551.                                     if (DoubleClick(lastfrontsecs, lastfrontmicros, secs, micros))
  552.                                         frontclicks++;
  553.                                     else frontclicks = 1;
  554.  
  555.                                     lastfrontsecs = secs;
  556.                                     lastfrontmicros = micros;
  557.                                     }
  558.                                 if (frontclicks == requiredfrontclicks) {
  559.                                     backorfront(FRONT);
  560.                                     if (requiredfrontclicks > 1) frontclicks = 0;
  561.                                     }
  562.                                 break;
  563.  
  564.                             case BACK:
  565.                                 if (requiredbackclicks > 1) {
  566.                                     if (DoubleClick(lastbacksecs, lastbackmicros, secs, micros))
  567.                                         backclicks++;
  568.                                     else backclicks = 1;
  569.  
  570.                                     lastbacksecs = secs;
  571.                                     lastbackmicros = micros;
  572.                                     }
  573.                                 if (backclicks == requiredbackclicks) {
  574.                                     backorfront(BACK);
  575.                                     if (requiredbackclicks > 1) backclicks = 0;
  576.                                     }
  577.                                 break;
  578.  
  579.                             }
  580.  
  581.                     case CXM_COMMAND:
  582.                         switch (msgid) {
  583.  
  584.                             case CXCMD_UNIQUE:
  585.                             case CXCMD_KILL:
  586.                                 quit = TRUE;
  587.  
  588.                             case CXCMD_DISABLE:
  589.                                 ActivateCxObj(broker, 0l);
  590.                                 break;
  591.  
  592.                             case CXCMD_ENABLE:
  593.                                 ActivateCxObj(broker, 1l);
  594.                                 break;
  595.  
  596.                             }
  597.                         break;
  598.  
  599.                     } /* switch msgtype */
  600.  
  601.                 } /* while CxMsg */
  602.  
  603.             } /* if (sigreceived & cxsigflag) */
  604.  
  605.         } /* while !quit */
  606.  
  607.     ActivateCxObj(broker, 0l);
  608. }
  609.  
  610. void backorfront(unsigned short mode)
  611. {
  612.     unsigned long lock;
  613.     register struct Screen *scr;
  614.     register struct Window *win;
  615.     register struct Layer *layer;
  616.  
  617.     #ifdef DEBUG
  618.     if (mode == BACK) printf("backorfront(BACK)\n");
  619.     else printf("backorfront(FRONT)\n");
  620.     #endif
  621.  
  622.     /*
  623.      *  here we go: find screen
  624.      */
  625.     lock = LockIBase(0l);
  626.     for (scr = IntuitionBase->FirstScreen;
  627.          scr && (scr->TopEdge > 0) && (scr->MouseY < 0);
  628.          scr = scr->NextScreen);
  629.  
  630.     if (scr) {
  631.  
  632.         #ifdef DEBUG
  633.         printf("backorfront(): scr = 0x%lx\n", scr);
  634.         #endif
  635.  
  636.         /*
  637.          *  get layer
  638.          */
  639.         LockLayerInfo(&scr->LayerInfo);
  640.         layer = WhichLayer(&scr->LayerInfo, (long)scr->MouseX, (long)scr->MouseY);
  641.         UnlockLayerInfo(&scr->LayerInfo);
  642.  
  643.         #ifdef DEBUG
  644.         printf("backorfront(): layer = 0x%lx\n", layer);
  645.         #endif
  646.  
  647.         if (layer && layer != scr->BarLayer) {
  648.  
  649.             if ((win = (struct Window *)layer->Window) && !(win->Flags & BACKDROP)) {
  650.  
  651.                 if (win->NextWindow || win->WScreen->FirstWindow != win) {
  652.  
  653.                     #ifdef DEBUG
  654.                     printf("backorfront(): using win 0x%lx for action\n", win);
  655.                     #endif
  656.  
  657.                     UnlockIBase(lock);
  658.                     if (mode == BACK) WindowToBack(win);
  659.                     else WindowToFront(win);
  660.  
  661.                     }
  662.  
  663.                 else UnlockIBase(lock);
  664.  
  665.                 } /* if win */
  666.  
  667.             else {
  668.                 /*
  669.                  *  no win or win = backdrop => switch screen to back
  670.                  */
  671.                 #ifdef DEBUG
  672.                 printf("backorfront(): using scr 0x%lx for action (no win)\n", scr);
  673.                 #endif
  674.  
  675.                 UnlockIBase(lock);
  676.                 if (mode == BACK) ScreenToBack(scr);
  677.                 else if (scr != IntuitionBase->FirstScreen) ScreenToFront(scr);
  678.  
  679.                 }
  680.  
  681.             } /* if layer */
  682.  
  683.         else {
  684.             /*
  685.              *  no layer:
  686.              */
  687.             #ifdef DEBUG
  688.             printf("backorfront(): using scr 0x%lx for action (no layer)\n", scr);
  689.             #endif
  690.  
  691.             UnlockIBase(0l);
  692.             if (mode == BACK) ScreenToBack(scr);
  693.             else if (scr != IntuitionBase->FirstScreen) ScreenToFront(scr);
  694.  
  695.             }
  696.  
  697.         } /* if scr */
  698.  
  699.     else UnlockIBase(0l);
  700.  
  701. }
  702.